﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
using System.Xml;
using System.Web;

namespace Util
{
    public class DataConvert
    {
        /// <summary>
        /// 将一个数组转换成Int32的数组
        /// </summary>
        /// <typeparam name="T">数组元素类型</typeparam>
        /// <param name="sourceArray">要转换的数组</param>
        /// <returns></returns>
        public static int[] ChangeToIntArray<T>(T[] sourceArray)
        {
            try
            {
                int[] m_TargetArray = new int[sourceArray.GetLength(0)];

                for (int i = 0; i < m_TargetArray.GetLength(0); i++)
                {
                    m_TargetArray[i] = Convert.ToInt32(sourceArray[i]);
                }
                return m_TargetArray;
            }
            catch
            {
                throw new Exception("<" + sourceArray.GetType().FullName + ">类型无法转换成<System.Int32>[]类型！");
            }
        }
        /// <summary>
        /// 将一个数组转换成String类型的数组
        /// </summary>
        /// <typeparam name="T">数组元素类型</typeparam>
        /// <param name="sourceArray">要转换的数组</param>
        /// <returns></returns>
        public static string[] ChangeToStringArray<T>(T[] sourceArray)
        {
            try
            {
                string[] m_TargetArray = new string[sourceArray.GetLength(0)];

                for (int i = 0; i < m_TargetArray.GetLength(0); i++)
                {
                    m_TargetArray[i] = Convert.ToString(sourceArray[i]);
                }
                return m_TargetArray;
            }
            catch
            {
                throw new Exception("<" + sourceArray.GetType().FullName + ">类型无法转换成<System.String>[]类型！");
            }
        }
        /// <summary>
        /// 将数组转换成一个用特定分割符分割的一个字符串
        /// </summary>
        /// <typeparam name="T">数组元素类型</typeparam>
        /// <param name="sourceArray">要转换的数组</param>
        /// <param name="Separator">连接各元素的符号（例如逗号，分号等等）</param>
        /// <returns></returns>
        public static string ChangeArrayToString<T>(T[] sourceArray, string Separator)
        {
            try
            {
                if (sourceArray == null) return null;

                string strResult = "";

                for (int i = 0; i < sourceArray.GetLength(0); i++)
                {
                    if (i == 0)
                    {
                        strResult = sourceArray[i].ToString();
                    }
                    else
                    {
                        strResult += Separator + sourceArray[i].ToString();
                    }
                }
                return strResult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 将List转换成一个用特定分割符分割的一个字符串
        /// </summary>
        /// <typeparam name="T">List元素类型</typeparam>
        /// <param name="sourceArray">要转换的List</param>
        /// <param name="Separator">连接各元素的符号（例如逗号，分号等等）</param>
        /// <returns></returns>
        public static string ChangeListToString<T>(List<T> SourceList, string Separator)
        {
            try
            {
                if (SourceList == null) return null;

                string strResult = "";

                for (int i = 0; i < SourceList.Count; i++)
                {
                    if (i == 0)
                    {
                        strResult = SourceList[i].ToString();
                    }
                    else
                    {
                        strResult += Separator + SourceList[i].ToString();
                    }
                }
                return strResult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 从DataTable对象中获取某列的值放到List对象当中
        /// </summary>
        /// <typeparam name="T">List对象元素类型</typeparam>
        /// <param name="m_DataTable">数据表对象</param>
        /// <param name="ColumnName">要获取数据的列名</param>
        /// <returns></returns>
        public static List<T> GetColumnDataToList<T>(System.Data.DataTable m_DataTable,string ColumnName)
        {
            List<T> m_List = new List<T>();
            try
            {
                for (int i = 0; i < m_DataTable.Rows.Count; i++)
                {
                    m_List.Add((T)m_DataTable.Rows[i][ColumnName]);
                }

                return m_List;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 转换日期对象到字符串
        /// </summary>
        /// <param name="dateTime">日期对象</param>
        /// <param name="FormatString">格式化字符串</param>
        /// <returns></returns>
        public static string DateTimeToString(DateTime dateTime, string FormatString)
        {
            if (dateTime.CompareTo(DateTime.MinValue) == 0)
            {
                return "";
            }
            else
            {
                if (string.IsNullOrEmpty(FormatString))
                {
                    return dateTime.ToString();
                }
                else
                {
                    return dateTime.ToString(FormatString);
                }
            }
        }
        /// <summary>
        /// 转换日期对象到字符串
        /// </summary>
        /// <param name="dateTime">日期对象</param>
        /// <returns></returns>
        public static string DateTimeToString(DateTime dateTime)
        {
            return DateTimeToString(dateTime, null);
        }
        /// <summary>
        /// 转换日期对象到短日期格式字符串
        /// </summary>
        /// <param name="dateTime">日期对象</param>
        /// <returns></returns>
        public static string DateTimeToShortDateString(DateTime dateTime)
        {
            if (dateTime.CompareTo(DateTime.MinValue) == 0)
            {
                return "";
            }
            else
            {
                return dateTime.ToShortDateString();
            }
        }
        /// <summary>
        /// 获取double类型的值
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <returns></returns>
        public static double GetDoubleValue(object objValue)
        {
            try
            {
                if (objValue == null || Convert.IsDBNull(objValue))
                {
                    return 0;
                }
                else
                {
                    return Math.Round(Convert.ToDouble(objValue), 3);
                }
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 获取 DateTime 类型的值，如果为空返回系统最小值
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <returns></returns>
        public static DateTime GetDateTimeValue(object objValue)
        {
            try
            {
                if (objValue == null || Convert.IsDBNull(objValue))
                {
                    return DateTime.MinValue;
                }
                else
                {
                    return Convert.ToDateTime(objValue);
                }
            }
            catch
            {
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// 获取 DateTime 类型的值，如果为空返回当前值
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <returns></returns>
        public static DateTime GetDateTimeValueOrNow(object objValue)
        {
            try
            {
                if (objValue == null || Convert.IsDBNull(objValue))
                {
                    return DateTime.Now;
                }
                else
                {
                    return Convert.ToDateTime(objValue);
                }
            }
            catch
            {
                return DateTime.Today;
            }
        }

        /// <summary>
        /// 获取Int32类型的值
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <returns></returns>
        public static System.Int32 GetInt32Value(object objValue)
        {
            try
            {
                if (objValue == null || Convert.IsDBNull(objValue))
                {
                    return 0;
                }
                else
                {
                    return Convert.ToInt32(objValue);
                }
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 获取Int32类型的值
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <returns></returns>
        public static int GetIntValue(object objValue)
        {
            try
            {
                if (objValue == null || Convert.IsDBNull(objValue))
                {
                    return 0;
                }
                else
                {
                    return int.Parse(objValue.ToString());
                }
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 获取Float类型的值
        /// </summary>
        /// <param name="objValue">值对象</param>
        /// <returns></returns>
        public static float GetFloatValue(object objValue)
        {
            try
            {
                if (objValue == null || Convert.IsDBNull(objValue))
                {
                    return 0;
                }
                else
                {
                    return float.Parse(objValue.ToString());
                }
            }
            catch
            {
                return 0;
            }
        }

    }
}
